/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.basic.park.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.basic.common.constant.enums.ParkBasicSysDictEnum;
import com.hopes.bizz.basic.park.dto.BaseShiftQueueDTO;
import com.hopes.bizz.basic.park.entity.BaseShiftQueueEntity;
import com.hopes.bizz.basic.park.entity.BaseShiftQueueRecordEntity;
import com.hopes.bizz.basic.park.entity.BaseShiftWorkEntity;
import com.hopes.bizz.basic.park.enums.SignInType;
import com.hopes.bizz.basic.park.mapper.BaseShiftQueueRecordMapper;
import com.hopes.bizz.basic.park.service.BaseShiftQueueRecordService;
import com.hopes.bizz.basic.park.service.BaseShiftQueueService;
import com.hopes.bizz.basic.park.service.BaseShiftWorkService;
import com.hopes.boss.admin.api.resolver.DictResolver;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 值班排班记录管理
 *
 * @author pig
 * @date 2024-01-24 10:40:12
 */
@Service
@RequiredArgsConstructor
public class BaseShiftQueueRecordServiceImpl extends ServiceImpl<BaseShiftQueueRecordMapper, BaseShiftQueueRecordEntity> implements BaseShiftQueueRecordService {


    private final BaseShiftWorkService baseShiftWorkService;
    private final BaseShiftQueueService baseShiftQueueService;

    @Override
    public R clockIn(BaseShiftQueueDTO baseShiftQueueDTO) {
        DateTime now = DateUtil.date();
        SignInType signInType = baseShiftQueueDTO.getSignInType();
        // 获取该班次的上下班时间
        BaseShiftWorkEntity shiftWorkEntity = baseShiftWorkService.getById(baseShiftQueueDTO.getShiftWorkId());
        Date workStart = shiftWorkEntity.getWorkStart();
        Date workEnd = shiftWorkEntity.getWorkEnd();
        // 签到状态
        String userStatus = signInType.switchStatus(now, workStart, workEnd);
        // 更新值班人员签到时间
        baseShiftQueueService.lambdaUpdate().eq(BaseShiftQueueEntity::getId, baseShiftQueueDTO.getShiftQueueId())
                .set(signInType.switchDate(), now).update();
        //新增记录
        BaseShiftQueueRecordEntity entity = new BaseShiftQueueRecordEntity();
        entity.setShiftQueueId(baseShiftQueueDTO.getShiftQueueId());
        entity.setShiftDate(now);
        entity.setShiftUserId(SecurityUtils.getUserId());
        entity.setRemarks("打卡成功：" + DictResolver.getItemLabelByValue(ParkBasicSysDictEnum.shift_queue_status,userStatus));
        baseMapper.insert(entity);
        return R.ok("签到成功");
    }
}
